package com.wang.designpattern.memento.game;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GameRole {

    private String name;
    // 攻击
    private int vit;
    // 防御
    private int def;

    public GameRole() {
    }

    public GameRole(String name, int vit, int def) {
        this.name = name;
        this.vit = vit;
        this.def = def;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getVit() {
        return vit;
    }

    public void setVit(int vit) {
        this.vit = vit;
    }

    public int getDef() {
        return def;
    }

    public void setDef(int def) {
        this.def = def;
    }

    // 创建Memento，即根据当前的状态得到Memento
    public Map<String, List<Memento>> createMemento(Caretaker caretaker) {
        Map<String, List<Memento>> map = caretaker.getRoleMementoListMap();
        if (CollectionUtils.isEmpty(map)) {
            map = new HashMap<>();
        }
        List<Memento> savedMementoList = caretaker.getRoleMementoListMap().get(this.getName());
        if (CollectionUtils.isEmpty(savedMementoList)) {
            savedMementoList = new ArrayList<>();
            map.put(this.getName(), savedMementoList);
        }
        savedMementoList.add(new Memento(vit, def));
        return map;
    }

    // 从备忘录对象恢复状态
    public void recoverGameRoleFromMemento(Map<String, List<Memento>> map, int index) {
        List<Memento> mementoList = map.get(this.getName());
        Memento memento = mementoList.get(index);
        this.vit = memento.getVit();
        this.def = memento.getDef();
    }

    // 显示当前游戏角色的状态
    public void display() {
        System.out.println(this.name + "当前的攻击力：" + this.vit + " 防御力：" + this.def);
    }
}
